<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="地块名称" prop="landNm">
        <el-input
          v-model="queryParams.landNm"
          placeholder="请输入地块名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种植作物" prop="cropCd">
        <el-input
          v-model="queryParams.cropCd"
          placeholder="请输入种植作物"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="土地性质" prop="landAttr">
        <el-select
          v-model="queryParams.land_attr"
          placeholder="请选择土地性质"
          clearable
        >
          <el-option
            v-for="dict in dict.type.land_attr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="灌溉方式" prop="irrMode">
        <el-select
          v-model="queryParams.irr_mode"
          placeholder="请选择灌溉方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.irr_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="灌水断面" prop="crossCd">
        <el-select v-model="queryParams.crossCd" placeholder="请选择灌水断面" clearable >
          <el-option
            v-for="dict in dmdata"
            :key="dict.crossCd"
            :label="dict.crossNm"
            :value="dict.crossCd"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地块状态" prop="landStatus">
        <el-select
          v-model="queryParams.land_status"
          placeholder="请选择地块状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.land_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datam:WiLandB:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datam:WiLandB:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datam:WiLandB:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datam:WiLandB:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="WiLandBList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地块名称" :show-overflow-tooltip="true" align="left" prop="landNm">
      </el-table-column>
      <el-table-column label="种植作物" align="center" prop="cropNm">
      </el-table-column>
      <el-table-column label="土地性质" align="center" prop="landAttr">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.land_attr"
            :value="scope.row.landAttr"
          />
        </template>
      </el-table-column>
      <el-table-column label="灌溉方式" align="center" prop="irrMode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.irr_mode" :value="scope.row.irrMode" />
        </template>
      </el-table-column>
      <el-table-column label="灌水断面" :show-overflow-tooltip="true" align="left" prop="crossNm">
      </el-table-column>
      <el-table-column label="地块状态" align="center" prop="landStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.land_status"
            :value="scope.row.landStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="地块面积(亩)" align="center" prop="landSize">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datam:WiLandB:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datam:WiLandB:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改地块信息登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body class="dkxx" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="地块名称" prop="landNm">
              <el-input v-model="form.landNm" placeholder="请输入地块名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="土地性质" prop="landAttr">
              <el-select v-model="form.landAttr" placeholder="请选择测站类型">
                <el-option
                  v-for="dict in dict.type.land_attr"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="灌溉方式" prop="irrMode">
              <el-select v-model="form.irrMode" placeholder="请选择测站类型">
                <el-option
                  v-for="dict in dict.type.irr_mode"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="种植作物" prop="cropCd">
              <el-input v-model="form.cropCd" placeholder="请输入种植作物" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="灌水断面" prop="crossCd">
              <el-select v-model="form.crossCd" placeholder="请选择灌水断面" clearable >
                <el-option
                  v-for="dict in dmdata"
                  :key="dict.crossCd"
                  :label="dict.crossNm"
                  :value="dict.crossCd"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地块状态" prop="landStatus">
              <el-select v-model="form.landStatus" placeholder="请选择测站类型">
                <el-option
                  v-for="dict in dict.type.land_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地块面积" prop="landSize">
              <el-input v-model="form.landSize" placeholder="请输入地块面积" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="landMark">
              <el-input v-model="form.landMark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWiLandB,
  getWiLandB,
  delWiLandB,
  addWiLandB,
  updateWiLandB,
} from "@/api/datam/WiLandB";
import { allListStCrossB } from "@/api/datam/StCrossB";

export default {
  name: "WiLandB",
  dicts: ["land_attr", "irr_mode", "land_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地块信息登记表格数据
      WiLandBList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        landNm: null,
        landAttr: null,
        crossCd: null,
        cropCd: null,
        irrMode: null,
        landStatus: null,
        landSize: null,
        landMark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dmdata:[],
    };
  },
  created() {
    this.getdmList();
    this.getList();
  },
  methods: {
    /** 查询断面列表 */
    getdmList() {
      this.loading = true;
      allListStCrossB(this.queryParams).then((response) => {
        this.dmdata = response.rows;
      });
    },
    /** 查询地块信息登记列表 */
    getList() {
      this.loading = true;
      listWiLandB(this.queryParams).then((response) => {
        this.WiLandBList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        landCd: null,
        landNm: null,
        landAttr: null,
        crossCd: null,
        cropCd: null,
        irrMode: null,
        landStatus: null,
        landSize: null,
        landMark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.landCd);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地块信息登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const landCd = row.landCd || this.ids;
      getWiLandB(landCd).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地块信息登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.landCd != null) {
            updateWiLandB(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWiLandB(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const landCds = row.landCd || this.ids;
      this.$modal
        .confirm('是否确认删除地块信息登记编号为"' + landCds + '"的数据项？')
        .then(function () {
          return delWiLandB(landCds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "datam/WiLandB/export",
        {
          ...this.queryParams,
        },
        `地块信息_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .cell {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
  line-height: 23px;
  padding-left: 10px;
  padding-right: 10px;
}

.dkxx{
  .el-select {
    width: 100%;
  }
}
</style>
